function beforeSaveEdit(){
    var b= NG.checkOnlySaveMain(['rzrq','pc'],'同一个项目一天只能填一次');
    return b;
}

function allReadyEdit() {
    NG.setFieldUnit('zdwdnew', '℃');
    NG.setFieldUnit('zgwdnew', '℃');
    NG.setFieldUnit('ldl', '人');
    if (otype == 'add' || otype == 'copy') {
        NG.getItem('rzrq').on('change', function (p1) {
            if (NG.getValue('rzrq') <= NG.getValue('bill_dt')) {
                var t = Math.floor((NG.getValue('bill_dt') - NG.getValue('rzrq')) / (24 * 3600 * 1000));
                if (t <= 3) {
                    var dStr = NG.dateFormat(p1.getValue(), 'YYYY-mm-dd');
                    NG.setValue('xq', weekDay(dStr));
                } else {
                    NG.alert('补录不得超过3天');
                    NG.setValue('rzrq', '', true);
                }
            } else {
                NG.alert('不能提前填写日志');
                NG.setValue('rzrq', '', true);
            }
        });
        NG.getItem('pc').on('helpselected', function (p1) {
            if (p1.data.CityId_EXName) {
                getWeatherByPc(p1.data.CityId_EXName);
            }
        });

        var v_bill_dt = NG.getValue('bill_dt');
        NG.setValue('rzrq', v_bill_dt);
    }
}

function weekDay(date) {
    var dt = new Date(date.split("-")[0], date.split("-")[1] - 1, date = date.split("-")[2]);
    var weekDay = ["天", "一", "二", "三", "四", "五", "六"];
    return weekDay[dt.getDay()];
}

function getWeatherByPc(cityName) {
    //获取时间差，只能获取今天和昨天的
    var t = Math.floor((NG.getValue('bill_dt') - NG.getValue('rzrq')) / (24 * 3600 * 1000));
    if (t <= 1) {
        //接口只能取今天和昨天数据
        var loadMarsk = new Ext.LoadMask(document.body, {msg: '天气获取中...', removeMask: true});
        loadMarsk.show();
        Ext.Ajax.request({
            method: 'GET',
            url: 'http://wthrcdn.etouch.cn/weather_mini?city=' + cityName,
            async: true, //异步请求
            success: function (response) {
                loadMarsk.hide();
                var high = '', low = '', ty = '';
                var resp = Ext.JSON.decode(response.responseText);
                if (resp.status == 1000) {
                    if (t == 0) {
                        //今天
                        high = getWdFromStr(resp.data.forecast[0].high);
                        low = getWdFromStr(resp.data.forecast[0].low);
                        ty = resp.data.forecast[0].type;
                    } else if (t == 1) {
                        //昨天
                        high = getWdFromStr(resp.data.yesterday.high);
                        low = getWdFromStr(resp.data.yesterday.low);
                        ty = resp.data.yesterday.type;
                    }
                    if (ty == '阴' || ty == '多云') {
                        ty = '2';
                    } else if (ty == '小雨' || ty == '中雨') {
                        ty = '3';
                    } else if (ty == '晴') {
                        ty = '1';
                    } else if (ty == '大雨' || ty == '暴雨') {
                        ty = '4';
                    }
                    NG.setValue('zgwdnew', high);
                    NG.setValue('zdwdnew', low);
                    NG.setValue('tqzk', ty);
                } else {
                    NG.alert('无法获取该项目所在城市天气，请自行填写');
                }
            },
            failure: function () {
                loadMarsk.hide();
            }
        });
    }
}

function getWdFromStr(wd) {
    return wd.substr(wd.indexOf(' ') + 1, wd.indexOf('℃') - wd.indexOf(' ') - 1)
}
